package com.tuni.birthdaymoney.persistence.entity;

// Generated 28.04.2013 16:40:29 by Hibernate Tools 3.4.0.CR1

import static javax.persistence.GenerationType.IDENTITY;

import java.beans.Transient;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Type;
import org.joda.time.DateTime;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;

import com.tuni.birthdaymoney.service.util.email.BirthdayUtils;

/**
 * BmPerson generated by hbm2java
 */
@Entity
@Table(name = "BM_Person")
public class Person implements java.io.Serializable {
    private String authToken;

    private Integer id;
    private Position position;
    private Department department;
    private String firstName;
    private String middleName;
    private String lastName;
    private String photoPath;
    private DateTime birthday;
    private String email;
    private boolean wantToReceive;
    private boolean wantToCelebrate;
    private boolean fired;
    private Set<Operation> operationsAsDonator = new HashSet<Operation>(0);
    private Set<Operation> operationsAsHumanResources = new HashSet<Operation>(0);
    private Set<Operation> operationsAsReceiptor = new HashSet<Operation>(0);
    private User user;

    @Column(name = "IsFired", nullable = false)
    public boolean isFired() {
        return fired;
    }

    public void setFired(boolean fired) {
        this.fired = fired;
    }

    public Person() {
    }

    public Person(Position position, Department department, String firstName, String lastName, DateTime birthday, String email,
            boolean wantToReceive, boolean wantToCelebrate) {
        this.position = position;
        this.department = department;
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthday = birthday;
        this.email = email;
        this.wantToReceive = wantToReceive;
        this.wantToCelebrate = wantToCelebrate;
    }

    public Person(Position position, Department department, String firstName, String middleName, String lastName, String photoPath,
            DateTime birthday, String email, boolean wantToReceive, boolean wantToCelebrate, Set<Operation> bmOperationsForDonatorId,
            Set<Operation> bmOperationsForHumanResourcesId, Set<Operation> bmOperationsForReceptorId, User user) {
        this.position = position;
        this.department = department;
        this.firstName = firstName;
        this.middleName = middleName;
        this.lastName = lastName;
        this.photoPath = photoPath;
        this.birthday = birthday;
        this.email = email;
        this.wantToReceive = wantToReceive;
        this.wantToCelebrate = wantToCelebrate;
        this.operationsAsDonator = bmOperationsForDonatorId;
        this.operationsAsHumanResources = bmOperationsForHumanResourcesId;
        this.operationsAsReceiptor = bmOperationsForReceptorId;
        this.user = user;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "ID", unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "PositionID", nullable = false)
    public Position getPosition() {
        return this.position;
    }

    public void setPosition(Position position) {
        this.position = position;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "DepartmentID", nullable = false)
    public Department getDepartment() {
        return this.department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    @Column(name = "FirstName", nullable = false, length = 60)
    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Column(name = "MiddleName", length = 60)
    public String getMiddleName() {
        return this.middleName;
    }

    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }

    @Column(name = "LastName", nullable = false, length = 60)
    public String getLastName() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Column(name = "PhotoPath", length = 100)
    public String getPhotoPath() {
        return this.photoPath;
    }

    public void setPhotoPath(String photoPath) {
        this.photoPath = photoPath;
    }

    @Temporal(TemporalType.DATE)
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    @DateTimeFormat(iso = ISO.DATE)
    @Column(name = "Birthday", nullable = false, length = 10)
    public DateTime getBirthday() {
        return this.birthday;
    }

    public void setBirthday(DateTime birthday) {
        this.birthday = birthday;
    }

    @Column(name = "Email", nullable = false, length = 60)
    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Column(name = "WantToReceive", nullable = false)
    public boolean isWantToReceive() {
        return this.wantToReceive;
    }

    public void setWantToReceive(boolean wantToReceive) {
        this.wantToReceive = wantToReceive;
    }

    @Column(name = "WantToCelebrate", nullable = false)
    public boolean isWantToCelebrate() {
        return this.wantToCelebrate;
    }

    public void setWantToCelebrate(boolean wantToCelebrate) {
        this.wantToCelebrate = wantToCelebrate;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "donator")
    public Set<Operation> getOperationsAsDonator() {
        return this.operationsAsDonator;
    }

    public void setOperationsAsDonator(Set<Operation> operationsAsDonator) {
        this.operationsAsDonator = operationsAsDonator;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "humanResources")
    public Set<Operation> getOperationsAsHumanResources() {
        return this.operationsAsHumanResources;
    }

    public void setOperationsAsHumanResources(Set<Operation> operationsAsHumanResources) {
        this.operationsAsHumanResources = operationsAsHumanResources;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "receiptor")
    public Set<Operation> getOperationsAsReceiptor() {
        return this.operationsAsReceiptor;
    }

    public void setOperationsAsReceiptor(Set<Operation> operationsAsReceiptor) {
        this.operationsAsReceiptor = operationsAsReceiptor;
    }

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = true)
    @PrimaryKeyJoinColumn
    public User getUser() {
        return this.user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Column(name = "AuthToken", nullable = false, length = 32, unique = true)
    public String getAuthToken() {
        return authToken;
    }

    public void setAuthToken(String authToken) {
        this.authToken = authToken;
    }

    @Transient
    public String createFullName() {
        return firstName + " " + lastName;
    }

    @Transient
    public String createFirstMiddleName() {
        if ((null != middleName) && !middleName.trim().isEmpty())
            return firstName + " " + middleName;
        else
            return firstName;
    }

    // @Transient
    // public int getNextAge(Calendar now) {
    // Calendar birthdayCalendar = birthday.toGregorianCalendar();
    // Calendar nextBirthday = BirthdayUtils.getNextBirthday(birthdayCalendar,
    // now);
    // return nextBirthday.get(Calendar.YEAR) -
    // birthdayCalendar.get(Calendar.YEAR);
    // }

}